package com.hao.tool.web.common;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hao.tool.web.module.fileInfo.entity.FileInfo;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-27 22:28
 */
public class FileInfoUtil {

    public static List<FileInfo> getList(String basePath) {
        List<FileInfo> fileInfoList = new ArrayList<>();
        List<File> fileList = FileUtil.loopFiles(basePath);
        fileList.forEach(file -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(IdWorker.getIdStr());
            fileInfo.setClickCount(0L);
            fileInfo.setCreateTime(LocalDateTime.now());
            fileInfo.setCreateUser("RuiHaoZ");
            fileInfo.setFilePath(file.getAbsolutePath());
            fileInfo.setFileName(file.getName());
            fileInfo.setFileType(FileUtil.getType(file));
            fileInfo.setFileSize(FileUtil.size(file));
            String fileMd5 = null;
            try {
                fileMd5 = DigestUtils.md5Hex(new FileInputStream(file.getAbsolutePath()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            fileInfo.setFileMd5(fileMd5);
            fileInfoList.add(fileInfo);
        });
        return fileInfoList;
    }

}
